package com.rimi.controller;

import com.rimi.bean.CarouselMap;
import com.rimi.bean.CommentSharing;
import com.rimi.bean.Doctor;
import com.rimi.bean.News;
import com.rimi.commons.CommonResult;
import com.rimi.model.dto.CommentDetailsDto;
import com.rimi.model.dto.CommentDto;
import com.rimi.service.IndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liHaiYang
 * @version V1.0
 * @Description: (用一句话描述该文件做什么)
 * @date 2021/7/5 10:12
 */
@Slf4j
@Controller
@CrossOrigin
@Api(description = "首页",tags = "首页")
public class IndexController {
    @Autowired
    IndexService indexService;

    @GetMapping("/search")
    @ResponseBody
    @ApiOperation("搜索文章")
    public CommonResult<List<News>> search(String str){
        List<News> list = indexService.search(str);
        return CommonResult.onSuccess(list);
    }

    @GetMapping("/comment")
    @ResponseBody
    @ApiOperation("问诊分享")
    public CommonResult<List<CommentDto>> comment(){
        List<CommentSharing> commentSharing = indexService.findCommentSharing();
        ArrayList<CommentDto> commentDtos = new ArrayList<>();
        for (CommentSharing sharing : commentSharing) {
            CommentDto commentDto = new CommentDto();
            commentDto.setDoctorNickname(sharing.getDoctor().getDoctorNickname());
            commentDto.setDoctorHeaderImg(sharing.getDoctor().getDoctorHeaderImg());
            commentDto.setUserNickName(sharing.getUser().getUserNickname());
            commentDto.setUserHeaderImg(sharing.getUser().getUserHeaderImg());
            BeanUtils.copyProperties(sharing,commentDto);
            commentDtos.add(commentDto);
        }
        return CommonResult.onSuccess(commentDtos);
    }

    @GetMapping("/indexcarousel")
    @ResponseBody
    @ApiOperation("首页顶部轮播图")
    public CommonResult<List<CarouselMap>> indexCarousel(){
        List<CarouselMap> newsById = indexService.findNewsById();
        return CommonResult.onSuccess(newsById);
    }

    @GetMapping("/veterinary")
    @ResponseBody
    @ApiOperation("热门兽医")
    public CommonResult<List<Doctor>> veterinary(){
        List<Doctor> doctors = indexService.findDoctors();
        return CommonResult.onSuccess(doctors);
    }

    @GetMapping("/comment/details")
    @ResponseBody
    @ApiOperation("问诊分享-详情")
    public CommonResult<ArrayList<CommentDetailsDto>> details(String name){
        List<CommentSharing> details = indexService.findCommentDetails(name);
        ArrayList<CommentDetailsDto> dtos = new ArrayList<>();
        for (CommentSharing detail : details) {
            CommentDetailsDto detailsDto = new CommentDetailsDto();
            detailsDto.setUserNickname(detail.getUser().getUserNickname());
            detailsDto.setUserHeaderImg(detail.getUser().getUserHeaderImg());
            detailsDto.setDoctorNickname(detail.getDoctor().getDoctorNickname());
            detailsDto.setDoctorHeaderImg(detail.getDoctor().getDoctorHeaderImg());
            detailsDto.setUserQuestion(detail.getUserQuestion());
            detailsDto.setDoctorAnswer(detail.getDoctorAnswer());
            detailsDto.setDiseaseType(detail.getDiseaseType());
            detailsDto.setPetNickname(detail.getPets().getPetNickname());
            detailsDto.setPetSex(detail.getPets().getPetSex());
            detailsDto.setPetType(detail.getPets().getPetType());
            detailsDto.setPetBirthday(detail.getPets().getPetBirthday());
            detailsDto.setPetWeight(detail.getPets().getPetWeight());
            detailsDto.setPetSterilization(detail.getPets().getPetSterilization());
            BeanUtils.copyProperties(detail,detailsDto);
            dtos.add(detailsDto);
        }
        return CommonResult.onSuccess(dtos);
    }

}
